package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.CircleDAO;
import com.wisewin.circle.dao.CircleFollowDAO;
import com.wisewin.circle.dao.TypeDAO;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.ActivityDO;
import com.wisewin.circle.entity.db.CircleDO;
import com.wisewin.circle.entity.db.CircleFollowDO;
import com.wisewin.circle.entity.db.TypeDO;
import com.wisewin.circle.entity.dto.ActivitySignUserDTO;
import com.wisewin.circle.util.PageInfo;
import com.wisewin.circle.util.PageObjectUtil;
import com.wisewin.circle.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@Transactional
public class CircleService {
    @Autowired
    private CircleDAO circleDAO;
    @Autowired
    private CircleFollowDAO circleFollowDAO;
    @Autowired
    private TypeDAO typeDAO;

    public ResultDTO queryTypeCircleList(Integer userId){
        List<TypeDO> lstType = circleDAO.queryTypeCircleList();
        // 查询圈子数据
        List<CircleDO> lstCircle = circleDAO.queryCircleDataList(userId);
        if(!CollectionUtils.isEmpty(lstCircle)){
            for(TypeDO typeDO : lstType){
                List<CircleDO> lstCircleThis = new ArrayList<>();
                for(CircleDO circleDO : lstCircle){
                    Integer ifFollow = circleDO.getUserId() == userId ? 1 : circleDO.getIfFollow();
                    Integer ifCanCancle = circleDO.getUserId() == userId ? 0 : 1;
                    circleDO.setIfFollow(ifFollow);
                    circleDO.setIfCanCancle(ifCanCancle);
                    // 处理图片
                    if(!StringUtils.isEmpty(circleDO.getCoverUrl())){
                        circleDO.setLstCoverUrl(Arrays.asList(circleDO.getCoverUrl().split(",")));
                    }
                    if(typeDO.getTypeId() == circleDO.getTypeId()){
                        lstCircleThis.add(circleDO);
                    }
                }
                typeDO.setLstCircle(lstCircleThis);
            }
        }
        return ResultDTOBuilder.success(lstType);
    }


    // 查询圈子列表
    public ResultDTO queryCircleInfoList(QueryParamBO queryParamBO, PageUtil pageUtil) {
        Map<String, Object> map = new HashMap<>();
        map.put("ifSelf", queryParamBO.getIfSelf());
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(circleDAO.queryCircleInfoCount(map), circleDAO.queryCircleInfoList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(CircleDO circleDO : (List<CircleDO>)pageData.getRecords()){
            // 处理图片
            if(!StringUtils.isEmpty(circleDO.getCoverUrl())){
                circleDO.setLstCoverUrl(Arrays.asList(circleDO.getCoverUrl().split(",")));
            }
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询圈子关注人列表
    public ResultDTO queryCircleFollowUserList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("circleId", queryParamBO.getCircleId());
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(circleDAO.queryCircleFollowUserCount(map), circleDAO.queryCircleFollowUserList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(ActivitySignUserDTO activitySignUserDTO : (List<ActivitySignUserDTO>)pageData.getRecords()){
            // 是否关注
            Integer ifFollow = activitySignUserDTO.getId() == queryParamBO.getUserId() ? 1 : activitySignUserDTO.getIfFollow();
            activitySignUserDTO.setIfFollow(ifFollow);
            activitySignUserDTO.setIfCanCancle(activitySignUserDTO.getId() == queryParamBO.getUserId() ? 0 : 1);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询圈子列表
    public ResultDTO queryCircleList(QueryParamBO queryParamBO, PageUtil pageUtil) {
        Map<String, Object> map = new HashMap<>();
        map.put("typeId", queryParamBO.getTypeId());
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(circleDAO.queryCircleCount(map), circleDAO.queryCircleList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(CircleDO circleDO : (List<CircleDO>)pageData.getRecords()){
            Integer ifFollow = circleDO.getUserId() == queryParamBO.getUserId() ? 1 : circleDO.getIfFollow();
            Integer ifCanCancle = circleDO.getUserId() == queryParamBO.getUserId() ? 0 : 1;
            circleDO.setIfFollow(ifFollow);
            circleDO.setIfCanCancle(ifCanCancle);
            // 处理图片
            if(!StringUtils.isEmpty(circleDO.getCoverUrl())){
                circleDO.setLstCoverUrl(Arrays.asList(circleDO.getCoverUrl().split(",")));
            }
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询圈子分类
    public ResultDTO queryCircleTypeList(){
        List<TypeDO> lstType = typeDAO.queryCircleTypeList();
        return ResultDTOBuilder.success(lstType);
    }

    // 查询热门圈子列表
    public ResultDTO queryCircleHotList(){
        List<CircleDO> lstCircle = circleDAO.queryCircleHotList();
        return ResultDTOBuilder.success(lstCircle);
    }

    // 查询圈子详细信息
    public ResultDTO queryCircleDetail(CircleDO circleDO){
        if(circleDO.getCircleId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        CircleDO circleOld = circleDAO.queryCircleDetail(circleDO.getCircleId());
        if(circleOld == null){
            return ResultDTOBuilder.failure("1", "圈子不存在", null);
        }
        CircleFollowDO circleFollowDO = circleFollowDAO.queryCircleFollowById(circleDO.getUserId(), circleDO.getCircleId());
        Integer ifFollow = circleDO.getUserId() == circleOld.getUserId() ? 1 : circleFollowDO == null ? 0 : 1;
        Integer ifCanCancle = circleDO.getUserId() == circleOld.getUserId() ? 0 : 1;
        circleOld.setIfFollow(ifFollow);
        circleOld.setIfCanCancle(ifCanCancle);
        // 处理图片
        if(!StringUtils.isEmpty(circleOld.getCoverUrl())){
            circleOld.setLstCoverUrl(Arrays.asList(circleOld.getCoverUrl().split(",")));
        }
        // 查询关注人信息
        List<String> lstHead = circleDAO.queryCircleFollowUser(circleDO.getCircleId());
        circleOld.setLstFollowUser(lstHead);
        return ResultDTOBuilder.success(circleOld);
    }

    // 取消关注圈子
    public ResultDTO cancleCircleFollow(CircleFollowDO circleFollowDO){
        if(circleFollowDO.getCircleId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        CircleDO circleDO = circleDAO.queryCircleById(circleFollowDO.getCircleId());
        if(circleDO == null){
            return ResultDTOBuilder.failure("1", "圈子不存在", null);
        }
        if(circleDO.getUserId().equals(circleFollowDO.getUserId())){
            return ResultDTOBuilder.failure("1", "本人创建圈子", null);
        }
        // 查询是否关注过圈子
        CircleFollowDO circleFollowOld = circleFollowDAO.queryCircleFollowById(circleFollowDO.getUserId(), circleFollowDO.getCircleId());
        if(circleFollowOld == null){
            return ResultDTOBuilder.failure("1", "未关注圈子", null);
        }
        // 删除关注
        circleFollowDAO.delete(circleFollowDO.getUserId(), circleFollowDO.getCircleId());
        // 修改圈子关注数量
        circleDAO.cancleFollowNum(circleFollowDO.getCircleId());
        return ResultDTOBuilder.success(circleFollowDO);
    }

    // 关注圈子
    public ResultDTO addCircleFollow(CircleFollowDO circleFollowDO){
        if(circleFollowDO.getCircleId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        CircleDO circleDO = circleDAO.queryCircleById(circleFollowDO.getCircleId());
        if(circleDO == null){
            return ResultDTOBuilder.failure("1", "圈子不存在", null);
        }
        if(circleDO.getUserId().equals(circleFollowDO.getUserId())){
            return ResultDTOBuilder.failure("1", "本人创建圈子", null);
        }
        // 查询是否关注过圈子
        CircleFollowDO circleFollowOld = circleFollowDAO.queryCircleFollowById(circleFollowDO.getUserId(), circleFollowDO.getCircleId());
        if(circleFollowOld != null){
            return ResultDTOBuilder.failure("1", "已关注圈子", null);
        }
        // 添加关注
        circleFollowDAO.insert(circleFollowDO);
        // 修改圈子关注数量
        circleDAO.updateFollowNum(circleFollowDO.getCircleId());
        return ResultDTOBuilder.success(circleFollowDO);
    }

    // 创建圈子
    public ResultDTO addCircle(CircleDO circleDO){
        if(StringUtils.isEmpty(circleDO.getName())){
            return ResultDTOBuilder.failure("1", "请填写圈子名称", null);
        }
        if(StringUtils.isEmpty(circleDO.getIntroduce())){
            return ResultDTOBuilder.failure("1", "请填写圈子简介", null);
        }
        if(StringUtils.isEmpty(circleDO.getHeadUrl())){
            return ResultDTOBuilder.failure("1", "请上传圈子头像", null);
        }
        if(StringUtils.isEmpty(circleDO.getCoverUrl())){
            return ResultDTOBuilder.failure("1", "请上传圈子封面", null);
        }
        if(circleDO.getPower() == null){
            return ResultDTOBuilder.failure("1", "请选择权限设置", null);
        }
        if(circleDO.getTypeId() == null){
            return ResultDTOBuilder.failure("1", "请选择圈子分类", null);
        }
        circleDO.setStatus(ActivityDO.STATUS_YFB);
        circleDAO.insert(circleDO);
        return ResultDTOBuilder.success(circleDO);
    }
}
